package com.cyf.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 日期工具
 * Created by cage on 2017/2/27.
 */
public class DateUtil {

    private static final String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    public final static String FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
    public final static String FORMAT_YYYYMMDD = "yyyyMMdd";
    public final static String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    /*
     * 秒 转化 天时分秒
     */
    public static String formatTime(Long ms) {
        if (ms == null || ms <= 0) {
            return "";
        }

        Integer mi = 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = ms - day * dd - hour * hh - minute * mi;

        StringBuffer sb = new StringBuffer();
        if (day != 0) {
            sb.append(day).append("天");
        }
        if (hour != 0) {
            sb.append(hour).append("小时");
        }
        if (second > 0) {
            minute++;
        }
        sb.append(minute).append("分钟");
//        sb.append(second+"秒");
        return sb.toString();
    }

    /**
     * 去掉时间的毫秒值
     *
     * @param dateStr 日期字符串(带毫秒或者不带毫秒值)
     * @return String 日期字符串，可能为null
     */
    public static String formatDateWithoutMillisecond(String dateStr) {
        String result = null;
        if (dateStr != null) {
            int indexOf = dateStr.indexOf(".");
            if (indexOf > 0) {
                result = dateStr.substring(0, indexOf);
            } else {
                result = dateStr;
            }
        }
        return result;
    }

    /**
     * 格式化当前时间
     *
     * @param pattern 表达式
     * @return String
     */
    public static String formatCurrentTime(String pattern) {
        return formatDate(new Date(), pattern);
    }

    /**
     * 根据格式 格式化时间
     *
     * @param date    时间
     * @param pattern 表达式
     * @return String
     */
    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * 获取某时间N秒之后的时间
     *
     * @param date    时间
     * @param seconds 秒
     * @return Date
     */
    public static Date getDateAfterSeconds(Date date, Integer seconds) {
        Date nextDate = new Date();
        nextDate.setTime(date.getTime() + seconds * 1000L);
        return nextDate;
    }

    /**
     * 根据格式 格式化时间
     *
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date parseDate(String dateStr, String pattern) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.parse(dateStr);
    }

    /**
     * 秒转日期(UTC时间转日期)
     *
     * @param seconds 秒
     * @return Date
     */
    public static Date convertSeconds2Date(int seconds) {
        // 秒转时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis((long) 1000 * seconds);
        return calendar.getTime();
    }

    /**
     * 日期转秒（日期转UTC时间）
     *
     * @param date 日期（为空时，取当前时间）
     * @return int 秒
     */
    public static int convertDate2Seconds(Date date) {
        if (date == null) {
            date = new Date();
        }
        return (int) (date.getTime() / 1000);
    }

    public static String formatTime(String str) {
        String time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            time = sdf.format(sdf.parse(str));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }


    /**
     * 日期转星期
     *
     * @param date
     * @return String
     */
    public static String getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); //当时间set 进calendar 里面
        int i = calendar.get(Calendar.DAY_OF_WEEK); //取星期
        return weeks[i - 1];
    }

    /**
     * 获取指定天数后的日期
     *
     * @param days 天数
     * @return Date
     */
    public static Date getAfterDay(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }

    public static String formatMinute(Integer minute) {
        if (minute == null || minute == 0) {
            return "0分钟";
        }

        int dayMinute = 24 * 60;

        int day = minute / (dayMinute);
        int hour = (minute - day * dayMinute) / 60;
        int remainMinute = (minute - day * dayMinute - hour * 60);

        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(day).append("天");
        }
        if (hour != 0) {
            sb.append(hour).append("小时");
        }
        if (remainMinute != 0) {
            sb.append(remainMinute).append("分钟");
        }
        return sb.toString();
    }


    public static String getFourRandom() {
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if (randLength < 4) {
            for (int i = 1; i <= 4 - randLength; i++)
                fourRandom = "0" + fourRandom;
        }
        return fourRandom;
    }


    public static String getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }
    public static String getFirstDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();
        String endDate = sdf.format(today);//当前日期
        //获取三十天前日期
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(today);
        theCa.add(theCa.DATE, -30);//最后一个数字30可改，30天的意思
        Date start = theCa.getTime();
        String startDate = sdf.format(start);//三十天之前日期
        return startDate;
    }


    /**
     * 转换日期到指定格式
     *
     * @param date    时间
     * @param pattern 日期格式
     * @return String
     */
    public static String convertDate2Str(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }
    public static Date getTimeTransformation(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = sdf.format(date);
        date1 = date1+" 23:59:59";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = null;
        try {
            time = sdf1.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return time;
    }

    public static String formatTimeNoZero(Long seconds) {
        seconds = seconds/1000;
        String timeStr = seconds/1000 + "秒";
        if (seconds > 60) {
            long second = seconds % 60;
            long min = seconds / 60;
            timeStr = min + "分" + second + "秒";
            if (min > 60) {
                min = (seconds / 60) % 60;
                long hour = (seconds / 60) / 60;
                timeStr = hour + "小时" + min + "分" + second + "秒";
                if (hour > 24) {
                    hour = ((seconds / 60) / 60) % 24;
                    long day = (((seconds / 60) / 60) / 24);
                    timeStr = day + "天" + hour + "小时" + min + "分" + second + "秒";
                }
            }
        }
        return timeStr;

    }

    /**
     * 转换指定格式的日期字符串到日期类型
     *
     * @param dateStr 日期字符串
     * @param pattern 日期格式
     * @return Date
     * @throws ParseException
     */
    public static Date convertStr2Date(String dateStr, String pattern) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.parse(dateStr);
    }
}
